package com.online.onlinestudent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.online.onlinestudent.constant.MessageConstant;
import com.online.onlinestudent.entity.Admin;
import com.online.onlinestudent.entity.Student;
import com.online.onlinestudent.entity.Teacher;
import com.online.onlinestudent.entity.User;
import com.online.onlinestudent.mapper.AdminMapper;
import com.online.onlinestudent.mapper.StudentMapper;
import com.online.onlinestudent.mapper.TeacherMapper;
import com.online.onlinestudent.service.ILoginService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public User doLogin(User user) {
        if (ObjectUtils.isEmpty(user)) {
            return null;
        }
        if (!StringUtils.hasLength(user.getUsername())){
            return null;
        }
        if (!StringUtils.hasLength(user.getPassword())) {
            return null;
        }
        //拿到用户名去数据库中查询

        List<Student> students;
        List<Teacher> teachers;
        List<Admin> admins;
        students = studentMapper.selectByUsername(user.getUsername());
        teachers = teacherMapper.selectByUsername(user.getUsername());
        admins = adminMapper.selectByUsername(user.getUsername());
        if ((students == null||students.isEmpty())&&(teachers == null||teachers.isEmpty())&&(admins == null||admins.isEmpty())) {
            return null;
        }

        if (!students.isEmpty()) {
            Student bean = students.get(0);
            if (ObjectUtils.isEmpty(bean)) {
                return null;
            }
            if (user.getPassword().equals(bean.getPassword())) {
                bean.setIsWho(0);
                return bean;
            }
        } else if (!teachers.isEmpty()) {
            Teacher bean = teachers.get(0);
            if (ObjectUtils.isEmpty(bean)) {
                return null;
            }
            if (user.getPassword().equals(bean.getPassword())) {
                bean.setIsWho(1);
                return bean;
            }
        }else if (!admins.isEmpty()) {
            Admin bean = admins.get(0);
            if (ObjectUtils.isEmpty(bean)) {
                return null;
            }
            if (user.getPassword().equals(bean.getPassword())) {
                bean.setIsWho(2);
                return bean;
            }
        }
        return null;
    }

    @Override
    public String doRegister(User user) {
        if (ObjectUtils.isEmpty(user)) {
            return MessageConstant.USER_EMPTY;
        }
        if (!StringUtils.hasLength(user.getUsername())){
            return MessageConstant.USERNAME_EMPTY;
        }
        if (!StringUtils.hasLength(user.getPassword())) {
            return MessageConstant.PASSWORD_EMPTY;
        }
        if (user.getIsWho() == 0) {
            QueryWrapper<Student> queryUsername = new QueryWrapper<>();
            QueryWrapper<Student> queryEmail = new QueryWrapper<>();
            queryUsername.eq("username", user.getUsername());
            queryEmail.eq("email", user.getEmail());
            if(!studentMapper.selectList(queryUsername).isEmpty())
            {
                return MessageConstant.USERNAME_DUPLICATE;
            }
            if (!studentMapper.selectList(queryEmail).isEmpty()) {
                return MessageConstant.EMAIL_DUPLICATE;
            }
            Student student = new Student();
            BeanUtils.copyProperties(user, student);
            studentMapper.insert(student);
            return MessageConstant.REGISTER_STUDENT_SUCCESS;
        }else if (user.getIsWho() == 1){
            QueryWrapper<Teacher> queryUsername = new QueryWrapper<>();
            QueryWrapper<Teacher> queryEmail = new QueryWrapper<>();
            queryUsername.eq("username", user.getUsername());
            queryEmail.eq("email", user.getEmail());
            if (!teacherMapper.selectList(queryUsername).isEmpty()) {
                return MessageConstant.USERNAME_DUPLICATE;
            }
            if(!teacherMapper.selectList(queryEmail).isEmpty())
            {
                return MessageConstant.EMAIL_DUPLICATE;
            }
            Teacher teacher = new Teacher();
            BeanUtils.copyProperties(user, teacher);
            teacherMapper.insert(teacher);
            return MessageConstant.REGISTER_TEACHER_SUCCESS;
        }else if (user.getIsWho() == 2){
            QueryWrapper<Admin> queryUsername = new QueryWrapper<>();
            QueryWrapper<Admin> queryEmail = new QueryWrapper<>();
            queryUsername.eq("username", user.getUsername());
            queryEmail.eq("email", user.getEmail());
            if (!adminMapper.selectList(queryUsername).isEmpty()){
                return MessageConstant.USERNAME_DUPLICATE;
            }
            if(!adminMapper.selectList(queryEmail).isEmpty())
            {
                return MessageConstant.EMAIL_DUPLICATE;
            }
            Admin admin = new Admin();
            BeanUtils.copyProperties(user, admin);
            adminMapper.insert(admin);
            return MessageConstant.REGISTER_ADMIN_SUCCESS;
        }
        return MessageConstant.UNKNOWN_ERROR;
    }
}
